package com.spring.sunflower.main.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.widget.NestedScrollView;

import com.bumptech.glide.Glide;
import com.google.android.flexbox.FlexboxLayout;
import com.spring.sunflower.R;
import com.spring.sunflower.anchor.PersonalDetailActivity;
import com.spring.sunflower.base.BaseFragment;
import com.spring.sunflower.bean.AnchorDetailBean;
import com.spring.sunflower.bean.BannerBean;
import com.spring.sunflower.common.BeautyPreviewActivity;
import com.spring.sunflower.common.HelpCenterActivity;
import com.spring.sunflower.common.PrivateChatSettingActivity;
import com.spring.sunflower.common.RelationActivity;
import com.spring.sunflower.common.SettingActivity;
import com.spring.sunflower.common.TaskActivity;
import com.spring.sunflower.common.WalletActivity;
import com.spring.sunflower.common.WebViewActivity;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.conversation.LatestVisitorActivity;
import com.spring.sunflower.dialog.LuckyMatchDialogUtils;
import com.spring.sunflower.event.LuckyMatchEvent;
import com.spring.sunflower.main.MainContract;
import com.spring.sunflower.main.adapter.ImageBannerAdapter;
import com.spring.sunflower.main.presenter.MePresenter;
import com.spring.sunflower.userinfo.AuthCenterActivity;
import com.spring.sunflower.util.DisplayUtils;
import com.spring.sunflower.util.StatusBarUtil;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/5/28<br/>
 * <p>
 * Describe:<br/>
 */
public class MeFragment extends BaseFragment<MePresenter> implements MainContract.MeView, View.OnClickListener {
    private RelativeLayout rlVIPCenter,
            rlMyWallet,
            rlAuthCenter,
            rlTask,
            rlInvite,
            rlBeauty,
            rlHelp;
    private TextView tvFansKey,
            tvFans,
            tvFollowedKey,
            tvFollowed,
            tvName,
            tvID,
            tvTitle,
            tvSign, tvMoneyNum;
    private ImageView ivAvatar, ivSetting;
    private String userId;
    private Banner banner;
    private List<BannerBean.DatasBean> bannerData = new ArrayList<>();
    private ImageBannerAdapter bannerAdapter;
    private FlexboxLayout flexboxLayout;
    private int[] colorArray = new int[6];
    private int[] iconArray = new int[]{R.drawable.ic_me_visitor,
//            R.drawable.ic_me_invite,
            R.drawable.ic_me_task, R.drawable.ic_me_chat, R.drawable.ic_me_beauty, R.drawable.ic_me_help};
    private List<String> itemList = new ArrayList<>();
    private ConstraintLayout clHeader;
    private LinearLayout llFans, llFollowed;
    private int mScrollY = 0;
    private NestedScrollView scrollView;
    private Toolbar toolbar;
    private ActionBar actionBar;
    private TextView tvAvatarChecking;
    private int count = 0;

    @Override
    public int setLayoutResId() {
        return R.layout.fragment_me;
    }

    @Override
    public void initView() {
//        tvTitle = mView.findViewById(R.id.tvTitle);
//        tvTitle.setText("我的");
        clHeader = mView.findViewById(R.id.clHeader);
        clHeader.setOnClickListener(this::onClick);
        ivSetting = mView.findViewById(R.id.ivSetting);
        ivSetting.setOnClickListener(this::onClick);
//        rlVIPCenter = mView.findViewById(R.id.rlVIPCenter);
        rlMyWallet = mView.findViewById(R.id.rlMyWallet);
        rlAuthCenter = mView.findViewById(R.id.rlAuthCenter);
//        rlTask = mView.findViewById(R.id.rlTask);
//        rlInvite = mView.findViewById(R.id.rlInvite);
//        rlBeauty = mView.findViewById(R.id.rlBeauty);
//        rlHelp = mView.findViewById(R.id.rlHelp);
        tvFansKey = mView.findViewById(R.id.tvFansKey);
        tvFans = mView.findViewById(R.id.tvFans);
        tvFollowedKey = mView.findViewById(R.id.tvFollowedKey);
        tvFollowed = mView.findViewById(R.id.tvFollowed);
        ivAvatar = mView.findViewById(R.id.ivCover);
        tvAvatarChecking = mView.findViewById(R.id.tvAvatarChecking);
        tvName = mView.findViewById(R.id.tvName);
        tvID = mView.findViewById(R.id.tvID);
        tvSign = mView.findViewById(R.id.tvMark);
        banner = mView.findViewById(R.id.banner);
        flexboxLayout = mView.findViewById(R.id.fblItem);
        llFans = mView.findViewById(R.id.llFans);
        llFollowed = mView.findViewById(R.id.llFollowed);
        tvMoneyNum = mView.findViewById(R.id.tvMoneyNum);
        toolbar = mView.findViewById(R.id.toolBar);
        tvTitle = mView.findViewById(R.id.tv_title);
        scrollView = mView.findViewById(R.id.scroll_view);
        tvTitle.setText("我的");
        scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            int lastScrollY = 0;
            int h = DisplayUtils.dip2px(mActivity, 170);
            int color = getColorX(R.color.colorPrimary) & 0x00ffffff;
            int titleColor = getColorX(R.color.white) & 0x00ffffff;

            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (lastScrollY < h) {
                    scrollY = Math.min(h, scrollY);
                    mScrollY = scrollY > h ? h : scrollY;
                    toolbar.setBackgroundColor(((255 * mScrollY / h) << 24) | color);
                    tvTitle.setTextColor((255 * mScrollY / h) << 24 | titleColor);
                }
                lastScrollY = scrollY;
            }
        });
        StatusBarUtil.immersive(mActivity);
        StatusBarUtil.setPaddingSmart(mActivity, toolbar);
        toolbar.setBackgroundColor(0);
        tvTitle.setTextColor(0);
        toolbar.setNavigationIcon(R.drawable.bg_shape_transparent);
//        rlVIPCenter.setOnClickListener(this);
        rlMyWallet.setOnClickListener(this);
        rlAuthCenter.setOnClickListener(this);
//        rlTask.setOnClickListener(this);
//        rlInvite.setOnClickListener(this);
//        rlBeauty.setOnClickListener(this);
//        rlHelp.setOnClickListener(this);
//        tvFans.setOnClickListener(this::onClick);
//        tvFansKey.setOnClickListener(this::onClick);
//        tvFollowed.setOnClickListener(this::onClick);
//        tvFollowedKey.setOnClickListener(this::onClick);
        llFans.setOnClickListener(this);
        llFollowed.setOnClickListener(this);
        userId = getField(Constant.KEY_USERID, "");
        tvID.setText(userId);
        tvName.setText(getField(Constant.KEY_NICKNAME, ""));
        bannerAdapter = new ImageBannerAdapter(mActivity, bannerData);
        banner.setAdapter(bannerAdapter);
//        banner.setBannerGalleryMZ(20);
//        banner.setBannerGalleryEffect(20, 10);
        itemList.add(getString(R.string.title_visitor));
//        itemList.add(getString(R.string.title_invite));
        itemList.add(getString(R.string.title_task));
        itemList.add(getString(R.string.title_chat_setting));
        itemList.add(getString(R.string.title_beauty_settings));
        itemList.add(getString(R.string.title_help_center));
        colorArray[0] = getColorX(R.color.orange);
        colorArray[1] = getColorX(R.color.deepSkyBlue);
        colorArray[2] = getColorX(R.color.green);
        colorArray[3] = getColorX(R.color.colorPrimary);
        colorArray[4] = getColorX(R.color.colorAccent);
        colorArray[5] = getColorX(R.color.khaki);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                BannerBean.DatasBean datasBean = bannerData.get(position);
                Intent intent = new Intent(mActivity, WebViewActivity.class);
                intent.putExtra(Constant.KEY_WEB_VIEW_TITLE, datasBean.getBannerName());
                intent.putExtra(Constant.KEY_WEB_VIEW_URL, datasBean.getJumpUrl());
                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        for (int i = 0; i < iconArray.length; i++) {
            View view = generateItem(iconArray[i], itemList.get(i), i);
            flexboxLayout.addView(view);
        }
    }

    @Override
    public void initListener() {

    }

    @Override
    public void loadData() {
        mPresenter.getBanner();
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getDetail(userId);
    }

    @Override
    public MePresenter setPresenter() {
        return new MePresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clHeader:
//                startNewActivity(UserInfoEditActivity.class);
                Intent intent = new Intent(getActivity(), PersonalDetailActivity.class);
                intent.putExtra(Constant.KEY_TARGET_ID, userId);
                startActivity(intent);
                break;
            case R.id.rlMyWallet:
                startNewCompatActivity(WalletActivity.class);
                break;
            case R.id.rlAuthCenter:
                startNewActivity(AuthCenterActivity.class);
                break;
            case R.id.ivSetting:
                startNewCompatActivity(SettingActivity.class);
                break;
            case R.id.llFans:
                goRelation(1);
                break;
            case R.id.llFollowed:
                goRelation(0);
                break;

        }
    }

    public void goRelation(int i) {
        Intent intent = new Intent(mActivity, RelationActivity.class);
        intent.putExtra(Constant.KEY_RELATION_TAB_POSITION, i);
        startActivity(intent);
    }

    @Override
    public void showData(AnchorDetailBean.DatasBean data) {
        printD("showData==================");
        AnchorDetailBean.DatasBean.VerifyVOBean verifyVO = data.getVerifyVO();
        Boolean photoHasVerify = verifyVO.getPhotoHasVerify();
        tvAvatarChecking.setVisibility(photoHasVerify ? View.VISIBLE : View.GONE);
        String photo = verifyVO.getPhoto();
        if (!TextUtils.isEmpty(photo)) {
            Glide.with(this).
                    load(photo)
                    .into(ivAvatar);
        } else {
            Glide.with(this).
                    load(data.getPhoto())
                    .into(ivAvatar);
        }
        tvID.setText("ID：" + data.getId());
        tvSign.setText(data.getSignature());
        tvFans.setText(data.getFans());
        tvFollowed.setText(data.getFavorite());
        tvMoneyNum.setText(data.getMoney());
        saveField(Constant.KEY_MONEY, data.getMoney());
        saveField(Constant.KEY_MOBILE, data.getMobile());
    }

    @Override
    public void showBannerData(List<BannerBean.DatasBean> data) {
        bannerAdapter.updateData(data);
    }

    public View generateItem(int id, String title, int index) {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.layout_me_item, null);
        ImageView icon = view.findViewById(R.id.ivIcon);
        icon.setColorFilter(colorArray[index]);
        TextView tv = view.findViewById(R.id.tvItem);
        LinearLayout linearLayout = view.findViewById(R.id.llContainer);
        int screenWidth = DisplayUtils.getScreenWidth(mActivity);
        int i = DisplayUtils.dip2px(mActivity, 61);
        int width = (screenWidth - i) / 3;
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, -2);
        layoutParams.width = width;
        view.setLayoutParams(layoutParams);
        Glide.with(mActivity)
                .load(id)
                .into(icon);
        tv.setText(title);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleItemClick(id);
            }
        });
        return view;
    }

    private void handleItemClick(int id) {
        switch (id) {
            case R.drawable.ic_me_visitor:
                startNewActivity(LatestVisitorActivity.class);
                break;
            case R.drawable.ic_me_beauty:
                startNewCompatActivity(BeautyPreviewActivity.class);
                break;
            case R.drawable.ic_me_help:
                startNewCompatActivity(HelpCenterActivity.class);
                break;
            case R.drawable.ic_me_chat:
                startNewCompatActivity(PrivateChatSettingActivity.class);
                break;
            case R.drawable.ic_me_invite:
                showToast("该功能待开放...");
                break;
            case R.drawable.ic_me_task:
//                ++count;
                startNewActivity(TaskActivity.class);
//                LuckyMatchEvent event = new LuckyMatchEvent();
//                event.setAge(20);
//                event.setAnchorId(10315);
//                event.setCity("西安市");
//                event.setMatchDegree(90);
//                event.setNickname("糖炒栗哈哈哈哈哈哈哈?");
//                event.setPhoto("https://yuyue-pro.oss-cn-shenzhen.aliyuncs.com/app/62392/3f05ab0dc2.png");
//                event.setType(1);
//                if (count % 2 == 0) {
//
//                LuckyMatchDialogUtils.getInstance().show(mActivity,event,"30");
//                }else {
//                LuckyMatchDialogUtils.getInstance().show(mActivity,event,"25");
//
//                }
                break;
        }
    }
}
